#include "BH1750.h"
#define WIFI_IOT_I2C_IDX_0 1
bool BH1750_WriteRegister(uint8_t address)
{
    uint8_t send_data[1] = {address};
    if(IoTI2cWrite(WIFI_IOT_I2C_IDX_0, (BH1750_DEVICE_ADDRESS << 1) | 0x00, &send_data,1) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool BH1750_Init(void)
{
    BH1750_WriteRegister(BH1750_POWER_OFF);
	BH1750_WriteRegister(BH1750_POWER_ON);
	BH1750_WriteRegister(BH1750_MODULE_RESET);
	BH1750_WriteRegister(BH1750_CONTINUE_MODE);
}

bool BH1750_GetData(float *lightIntensity)
{
    uint8_t buffer[2];
    if(IoTI2cRead(WIFI_IOT_I2C_IDX_0, (BH1750_DEVICE_ADDRESS << 1) | 0x01, &buffer,sizeof(buffer)) == 0)
    {
        uint16_t data = ((uint16_t)buffer[0] << 8) + buffer[1];
        *lightIntensity = (float)data / 1.2 * RESOLURTION;
        return true;
    }
    else
    {
        *lightIntensity = 0;
        return false;
    }
}
